#    Copyright 2012 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPWeb.
#    COOPWeb is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPWeb is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPWeb.  If not, see <http://www.gnu.org/licenses/>.

import os
import datetime
import shutil

#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/COOPWeb/djangoproject/include', '/opt/COOPWeb/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
from django.contrib.gis.geos import *
import os, time, psycopg2, datetime
#********************Setup psycopg2 and django section********************
PathDjangoRoot="/opt/COOPWeb/"
if(not PathDjangoRoot in sys.path):
    sys.path.append(PathDjangoRoot)
from django.core.management import setup_environ
import djangoproject
from djangoproject import settings
setup_environ(settings)
from models import *
#********************Setup psycopg2 and django section********************

from django.template import Context, loader
from django.http import HttpResponse
from django import forms
import os
import datetime
import djangoproject.settings
import COOPWeb_Misc, COOPWeb_Form
import cPickle






def BuildAlarmTableForm(SpecialBuildFormDataDict):
    Dict={}
    #Dict['PingMeterCommand'] = forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'12'}))
    for i in range(0, len(SpecialBuildFormDataDict['Alarms'])):
        Dict['UpdateIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['id' + str(i)]=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'5', 'READONLY':'True'}))
        Dict['Alarmid' + str(i)]=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'5', 'READONLY':'True'}))
        Dict['AlarmArmIndicator' + str(i)]=forms.BooleanField()
        Dict['AlarmDisarmIndicator' + str(i)]=forms.BooleanField()
        Dict['AlarmAcknowledgeIndicator' + str(i)]=forms.BooleanField()
        Dict['AlarmCloseIndicator' + str(i)] = forms.BooleanField()
        Dict['AlarmEventDescription' + str(i)]=forms.CharField(required=False, widget=forms.Textarea(attrs={'rows':'2', 'cols':'50', 'READONLY':'True'}))
        Dict['AlarmMessage' + str(i)]=forms.CharField(required=False, widget=forms.Textarea(attrs={'rows':'2', 'cols':'30'}))

    return Dict

def ProcessAlarmTableForm(form, Username):
    i = 0
    while(True):
        if(not form.data.has_key('Alarmid' + str(i))):
            break

        UpdateIndicator=COOPWeb_Form.FormGetBoolean(form, 'UpdateIndicator' + str(i))
        if(UpdateIndicator):
            id = COOPWeb_Form.FormGetInteger(form, 'id' + str(i))
            alarmid=COOPWeb_Form.FormGetInteger(form, 'Alarmid' + str(i))
            alarm=djangoproject.models.Alarm.objects.filter(id=id)[0]
            AlarmArmIndicator=COOPWeb_Form.FormGetBoolean(form, 'AlarmArmIndicator' + str(i))
            
            AlarmDisarmIndicator=COOPWeb_Form.FormGetBoolean(form, 'AlarmDisarmIndicator' + str(i))
            AlarmAcknowledgeIndicator=COOPWeb_Form.FormGetBoolean(form, 'AlarmAcknowledgeIndicator' + str(i))
            AlarmCloseIndicator=COOPWeb_Form.FormGetBoolean(form, 'AlarmCloseIndicator' + str(i))
            AlarmDescription=COOPWeb_Form.FormGetString(form, 'AlarmEventDescription' + str(i))
            AlarmMessage=COOPWeb_Form.FormGetString(form, 'AlarmMessage' + str(i))
            #Do update stuff here

            if(bool(alarm.CurrentEventid)):
                if(AlarmAcknowledgeIndicator):
                    djangoproject.models.CommunicationCommand.CreateAlarmEventAcknowledgeCommand(alarmid, AlarmMessage, Username)
                if(AlarmArmIndicator):
                    djangoproject.models.CommunicationCommand.CreateAlarmArmCommand(alarmid, AlarmMessage, Username)
                if(AlarmDisarmIndicator):
                    djangoproject.models.CommunicationCommand.CreateAlarmDisarmCommand(alarmid, AlarmMessage, Username)
                if(AlarmCloseIndicator):
                    djangoproject.models.CommunicationCommand.CreateAlarmEventCloseCommand(alarmid, AlarmMessage, Username)
            else:  #If no alarm event, just allow the alarm to be armed and disarmed
                if(AlarmArmIndicator):
                    djangoproject.models.CommunicationCommand.CreateAlarmArmCommand(alarmid, AlarmMessage, Username)
                if(AlarmDisarmIndicator):
                    djangoproject.models.CommunicationCommand.CreateAlarmDisarmCommand(alarmid, AlarmMessage, Username)
        i = i + 1  #Next Alarm

def PopulateAlarmTableForm(form, Alarms):
    for i in range(0, len(Alarms)):
        form.data['UpdateIndicator' + str(i)]=False
        #form.data['Alarmid' + str(i)]=Alarms[i].id
        form.setData({'id' + str(i): Alarms[i].id})
        form.setData({'Alarmid' + str(i): Alarms[i].Alarmid})
        form.setData({'AlarmArmIndicator' + str(i): False})
        form.setData({'AlarmDisarmIndicator' + str(i): False})
        form.setData({'AlarmAcknowledgeIndicator' + str(i): False})
        form.setData({'AlarmCloseIndicator' + str(i): False})

        form.setData({'AlarmEventDescription' + str(i): Alarms[i].CurrentEventNote})
        form.setData({'AlarmMessage' + str(i): ''})
    return form



